/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.testdesign;

import java.util.HashMap;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestSuiteRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author nurhat
 */
public class TestSuiteDataHandler extends TestPartDataHandler {

    @Override
    public TestDesignTreeNode operationAdd(HashMap<String, Object> params,
	    TestDesignTreeNode parentNode, int childIndex) {
	TestDesignTreeNode newNode = null;
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String owner = (String) params.get("owner");
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		T_test_stepcontainer container = new T_test_stepcontainer();
		container.setName(name);
		container.setDescription(description);
		container.setContainerTypeId(TestDesign
			.getDesignPartTypeIdByName("TestSuite"));
		container.save();

		T_test_designtree obj = new T_test_designtree();
		obj.save();
		TestDesignTreeRefined designTree = TestDesignTreeRefined
			.getDesignTree(obj);
		designTree.getTreeReference().setTypeId(
			container.getContainerTypeId());
		designTree.getTreeReference().setElementId(container.getId());
		designTree.getTreeReference().setRank(null);
		designTree.getTreeReference().save();

		if (owner != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), null);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.ThreadPoolSize.name(), null);
		designTree.setParameterValue(
			TestDesignTreeParameters.Timeout.name(), null);
		designTree
			.setParameterValue(
				TestDesignTreeParameters.SuccessPercentage
					.name(), null);

		designTree.getTreeReference().save();

		TestSuiteRefined result = new TestSuiteRefined(container);
		newNode = TestDesignTreeNode.getNode(designTree, result);
		t.commit();
		TestDesign.m_testSuites.put(container.getName(), container);
	    } catch (PersistentException ex) {
		t.rollback();

		ex.printStackTrace();
		newNode = null;
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	    newNode = null;
	}
	return newNode;
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
	    TestDesignTreeNode node) {
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String owner = (String) params.get("owner");
	TestSuiteRefined suite = (TestSuiteRefined) node.getObject();
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		T_test_stepcontainer container = suite.getReference();
		String oldName = container.getName();
		container.setName(name);
		container.setDescription(description);
		container.save();
		if (owner != null) {
		    node.getReference().setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		    node.getReference().getTreeReference().save();
		} else {
		    node.getReference().setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), null);
		    node.getReference().getTreeReference().save();
		}
		t.commit();
		TestDesign.m_testSuites.remove(oldName);
		TestDesign.m_testSuites.put(container.getName(), container);
		return true;
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
	return false;
    }
}
